import net.es.lookup.client.QueryClient;
import net.es.lookup.client.SimpleLS;
import net.es.lookup.common.exception.LSClientException;
import net.es.lookup.common.exception.ParserException;
import net.es.lookup.common.exception.QueryException;
import net.es.lookup.queries.Query;
import net.es.lookup.records.Record;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 7/7/13
 * Time: 7:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExampleQueryClient {

    /*
    This example demonstrates the use of the QueryClient. The purpose of the
    QueryClient is to provide methods for querying the Simple Lookup Service
    for records that match specified parameters.
     */

    public static void main(String[] args) {

        String host = "localhost";
        int port = 8090;

        try {
            /*
            An instance of SimpleLS first needs to be instantiated. This will
            be passed to the QueryClient constructor.
            */
            SimpleLS sls = new SimpleLS(host, port);
            QueryClient qc = new QueryClient(sls);

            /*
            An instance of Query is needed to perform a query on the SimpleLS.
            The Query instance can be passed to the QueryClient via the
            setQuery method. (Note: the Query need not have a keyValues map.)
             */
            Query query = new Query();
            qc.setQuery(query);

            /*
            Alternatively, the Query instance can be passed to the
            QueryClient in the constructor arguments.
             */
            qc = new QueryClient(sls, query);

            /*
            The QueryClient performs the query operation for the specified
            Query using the query method. The method returns a List of Records
            that contains the records that matched the specified parameters.
             */
            List<Record> recordList;
            recordList = qc.query();

        } catch (LSClientException e) {

            /*
            Both the SimpleLS and QueryClient throw LSClientExceptions.
             */
        } catch (ParserException e) {

            /*
            The QueryClient throws a ParserException if the query result
            cannot be parsed to valid Records.
             */
        } catch (QueryException e) {

            /*
            The Query class throws a QueryException when its toURL method is
            used. This method is used twice by the QueryClient: when its
            constructor is passed a Query and when its setQuery method is
            called. In both cases, the provided Query will throw the exception
            if it contains invalid operator values (anything other than "ANY"
            or "ALL").
             */
        }
    }
}
